/**
 * King.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public class King extends Piece {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty king.
 	 */
	public King() {
		super();
	}

	/**
	 * Initializes a new king of a given color.
	 * 
	 * @param white true if the king must be white, false if it must be black.
	 */
	public King(boolean white) {
		super(white);
	}
	
	/**
	 * Initializes a new king with all of its attributes.
	 * 
	 * @param x	the x coordinate of the king on the chessboard.
	 * @param y the y coordinate of the king on the chessboard.
	 * @param white true if the king must be white, false if it must be black.
	 */
	public King(int x, int y, boolean white) {
		super(x, y, white);
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new king from a given one.
	 * 
	 * @param other the king to copy in the new one.
	 */
	public King(King other) {
		super(other);
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the king to move on the chessboard.
	 * @param y the y coordinate where we want the king to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = (cb.isOutOfRange(x, y) && differentPosition(x, y));
		
		// Specific to this piece:
		if(isValid && ((x < getX() - 1 || x > getX() + 1) || (y < getY() - 1 || y > getY() + 1))) isValid = false;
		
		if(isValid && cb.getPiece(x, y) != null && act) {	// The piece can be taken.
			if(cb.getPiece(x, y).isWhite() != isWhite()) {
				cb.removePiece(x, y);
			}
			else isValid = false;	// There is already a piece on this square.
		}
		
		return isValid;
	}

	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'K';
		else return 'k';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public Piece getCopy() {
		King copy = new King(m_x, m_y, m_white);
		
		return copy;
	}
	
	//-----------------------------------------------------------------------------------------
}
